package com.linkoog.devtools.http.toolwindow.workbenche.left.http_service;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.actionSystem.ex.CheckboxAction;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.linkoog.devtools.http.toolwindow.workbenche.left.http_service.toolbar.ModuleFilterPopup;
import com.linkoog.devtools.intellij.ui.tree.ListTreeWithToolbarPanel;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.event.HierarchyEvent;
import java.util.ArrayList;
import java.util.List;

public class HttpServiceTreeToolbarActionsProvider implements ListTreeWithToolbarPanel.DefaultActionGroupProvider {

    private final Project project;


    public HttpServiceTreeToolbarActionsProvider(Project project) {
        this.project = project;
    }

    public DefaultActionGroup get(ListTreeWithToolbarPanel listTreeWithToolbarPanel) {
        DefaultActionGroup actionGroup = new DefaultActionGroup();
        actionGroup.addSeparator();
        actionGroup.add(createRefreshAction(listTreeWithToolbarPanel));
        actionGroup.add(createModuleFilterAction(listTreeWithToolbarPanel));

        actionGroup.addSeparator();
        actionGroup.add(createSwaggerViewAction(listTreeWithToolbarPanel));
        actionGroup.add(createSearchAction(listTreeWithToolbarPanel));
        return actionGroup;
    }

    private AnAction createRefreshAction(ListTreeWithToolbarPanel listTreeWithToolbarPanel){
        return new DumbAwareAction("Refresh", "", AllIcons.Actions.Refresh){
            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                listTreeWithToolbarPanel.refreshRestServiceTree();
            }

            @Override
            public void update(@NotNull AnActionEvent e) {
                e.getPresentation().setEnabled(listTreeWithToolbarPanel.getTree().isEnabled());
            }

            @Override
            public @NotNull ActionUpdateThread getActionUpdateThread() {
                return ActionUpdateThread.BGT;
            }
        };
    }

    private AnAction createModuleFilterAction(ListTreeWithToolbarPanel listTreeWithToolbarPanel){
        return new DumbAwareAction("Module Filter", "", AllIcons.General.Filter){
            private ModuleFilterPopup filterPopup;
            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                JTree httpServiceTree = listTreeWithToolbarPanel.getTree();
                ModuleFilterPopup filterPopup = getModuleFilterPopup();
                filterPopup.setPopupSize(httpServiceTree.getWidth(), filterPopup.getHeight());
                filterPopup.show(httpServiceTree, 0, filterPopup.getY());
            }

            private ModuleFilterPopup getModuleFilterPopup(){
                JTree httpServiceTree = listTreeWithToolbarPanel.getTree();
                Module[] modules = ModuleManager.getInstance(project).getModules();
                if (this.filterPopup != null && modules.length == this.filterPopup.getModuleNameList().size()){
                    return this.filterPopup;
                }

                List<String> moduleNameList = new ArrayList<>();
                for (Module module : modules) {
                    moduleNameList.add(module.getName());
                }

                filterPopup = new ModuleFilterPopup(moduleNameList);
                filterPopup.addHierarchyListener(e -> {
                    if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
                        if (! e.getComponent().isShowing()) {
                            ((HttpServicePanel) listTreeWithToolbarPanel).resetSelectedModuleList(filterPopup.getSelectedValues());
                            listTreeWithToolbarPanel.refreshRestServiceTree();
                        }
                    }
                });
                return filterPopup;
            }

            @Override
            public void update(@NotNull AnActionEvent e) {
                e.getPresentation().setEnabled(listTreeWithToolbarPanel.getTree().isEnabled());
            }

            @Override
            public @NotNull ActionUpdateThread getActionUpdateThread() {
                return ActionUpdateThread.BGT;
            }
        };
    }

    private AnAction createSearchAction(ListTreeWithToolbarPanel listTreeWithToolbarPanel){
        return new DumbAwareAction("Search", "", AllIcons.Actions.Find){
            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                ActionManager actionManager = ActionManager.getInstance();
                AnAction searchApiAction = actionManager.getAction("com.github.linkoog.ApiDevTools.SearchApiAction");
                if (searchApiAction != null){
                    searchApiAction.actionPerformed(e);
                }
            }

            @Override
            public void update(@NotNull AnActionEvent e) {
                e.getPresentation().setEnabled(listTreeWithToolbarPanel.getTree().isEnabled());
            }

            @Override
            public @NotNull ActionUpdateThread getActionUpdateThread() {
                return ActionUpdateThread.BGT;
            }
        };
    }

    private AnAction createSwaggerViewAction(ListTreeWithToolbarPanel listTreeWithToolbarPanel){
        return new CheckboxAction("Swagger视图") {
            @Override
            public boolean isSelected(@NotNull AnActionEvent e) {
                Object showSwaggerView =  listTreeWithToolbarPanel.getTree().getClientProperty(HttpServicePanel.SHOW_SWAGGER_KEY);
                return Boolean.TRUE.equals(showSwaggerView);
            }

            @Override
            public void setSelected(@NotNull AnActionEvent e, boolean state) {
                listTreeWithToolbarPanel.getTree().putClientProperty(HttpServicePanel.SHOW_SWAGGER_KEY, state);
                listTreeWithToolbarPanel.refreshRestServiceTree();
            }

            @Override
            public @NotNull ActionUpdateThread getActionUpdateThread() {
                return ActionUpdateThread.BGT;
            }

            @Override
            public void update(@NotNull AnActionEvent e) {
                e.getPresentation().setEnabled(listTreeWithToolbarPanel.getTree().isEnabled());
            }

        };
    }
}
